# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2023-06-12
desc: 
sample: 
"""
import json #系统自带 python


import jsonpath
import requests  #第三方提供   HTML  xml  正则
import urllib3

host = 'http://82.156.74.26:9088/pinter'

#接口测试 requests 工具  返回的问题
def test_get():
    url = host +'/com/getSku?id=1'  #url
    #url = host + '/com/getSku'
    #get参数 很多参数  json =dict
    #params = {"id":1}
    response = requests.get(url=url, verify=False)
    print(response.status_code)
    assert response.status_code == 200  #断言
    if response.status_code == 200:
       response_str = response.text  #str
       # dict = json.loads(response_str)
       # print(response.text)
       #如果是字典
       # 字符串转json
       dict = response.json()  #转化
       print(dict)
       print(type(dict.get('data')))
       print(dict.get('data').get('skuId'))
       print(dict['data']['skuId'])
       assert dict.get('code')=='0'
       #注意 list
       id = jsonpath.jsonpath(dict,'$.data.skuId')
       print(id[0])


def test_post():
    url = host + '/com/login'
    #post 参数 很多参数
    data = {"userName":'admin','password':'1234'}
    response = requests.post(url=url,data=data) #body  https加密 post
    assert response.status_code == 200  # 断言
    dict = response.json()  # 转化
    print(dict)
    assert dict.get('message') == 'success'

def test_postjson():
    url = host + '/com/register'
    #post 参数 很多参数
    data = {"userName":"test","password":"1234","gender":1,"phoneNum":"110","email":"beihe@163.com","address":"Beijing"}
    print(type(data))
    requests.post(url=url,json=data) #body  https加密 post


def test_postxml():
    url = 'http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx'
    #post 参数 很多参数
    xml ='<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' \
         '<soap:Body><getMobileCodeInfo xmlns="http://WebXml.com.cn/">' \
         '<mobileCode>1880987234</mobileCode>' \
         '<userID></userID>' \
         '</getMobileCodeInfo></soap:Body></soap:Envelope>'
    print(type(xml))
    xml_header = {'Content-type': 'text/xml'}
    requests.post(url=url,headers=xml_header,data=xml) #body  https加密 post

#pytest main 不生效
if __name__ == '__main__':
    # print(type(str))
    # books = json.loads(str)
    # checkurl = "$.store.bicycel.color"
    # print(jsonpath.jsonpath(books, checkurl))
    #test_get()
    #test_post()
    #test_postjson()
    #test_postxml()
    pass